#pragma once
#include "../server.hpp"
class EchoServer
{
public:
    EchoServer(int port):_server(port)
    {
        _server.SetCloseCallback(std::bind(&EchoServer::OnClosed,this,std::placeholders::_1));
        _server.SetConnectedCallback(std::bind(&EchoServer::OnClosed,this,std::placeholders::_1));
        _server.SetMessageCallback(std::bind(&EchoServer::OnMessage,this,std::placeholders::_1,std::placeholders::_2));
        _server.SetThreadCount(2);
        _server.EnableInactiveRelease(1);
    }
    void Start()
    {
        _server.Start();
    }
private:
    void Onconnected(const PtrConnection &conn)
    {
        DEBUG_LOG("NEW CONNECTION");
    }
    void OnClosed(const PtrConnection &conn)
    {
        DEBUG_LOG("Close CONNECTION");
    }
    void OnMessage(const PtrConnection &conn, Buffer *buf)
    {
        DEBUG_LOG("%d,receive:%s", conn->Id(),buf->Read_Position());
        conn->Send(buf->Read_Position(), buf->ReadAbleSize());
        buf->MoveReadOffset(buf->ReadAbleSize());
    }

private:
    TcpServer _server;
};